package com.huatai.datadevelop.controller;

import com.alibaba.fastjson.JSON;
import com.huatai.common.api.R;
import com.huatai.datadevelop.entity.TableManageEntity;
import com.huatai.datadevelop.entity.TokenConfigEntity;
import com.huatai.datadevelop.service.TableManageService;
import com.huatai.datadevelop.service.TokenConfigService;
import com.huatai.datadevelop.utils.PageUtils;
import com.huatai.datadevelop.vo.DeleteVO;
import com.huatai.datadevelop.vo.KeyValueVO;
import com.huatai.datadevelop.vo.TableNameComplianceVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author hecai
 * @description: TODO
 * @date 2022/11/23 9:43
 * @Version 1.0
 */
@Api(value = "第三方token配置",tags = "第三方token配置")
@RestController
@RequestMapping("/token/manage")
public class TokenManageController {

    @Autowired
    private TokenConfigService tokenConfigService;

	@Autowired
	private TableManageService tableManageService;

    /**
     * 验证token是否请求成功
     * @return
     */
	@ApiOperation("验证token是否请求成功")
    @GetMapping("getApiTokenByBody/{id}")
    public R getApiTokenByBody(@PathVariable("id") Integer id){
        return R.data(tokenConfigService.getApiTokenByBody(id));
    }

	@ApiOperation("获取token数据")
    @PostMapping("getApiTokenCheck")
    public R getApiTokenCheck(@RequestBody TokenConfigEntity data){
        return R.data(tokenConfigService.getApiTokenCheck(data));
    }

    /**
     * 新增或修改
     * @param data
     * @return
     */
	@ApiOperation("新增或修改")
    @PostMapping("/saveOrUpdateTokenConfig")
    public R saveOrUpdateTokenConfig(@RequestBody TokenConfigEntity data){
        this.tokenConfigService.saveOrUpdateTokenConfig(data);
        return R.success("成功");
    }

    /**
     * 删除或批量删除
     * @param data
     * @return
     */
	@ApiOperation("删除或批量删除")
    @PostMapping("/delete")
    public R delete(@RequestBody DeleteVO data){
		Boolean aBoolean = this.tokenConfigService.deleteByIds(JSON.parseArray(JSON.toJSONString(data.getIds()), Integer.class));
		return aBoolean?R.success("删除成功"):R.fail("删除失败");
    }

    /**
     * 获取列表
     */
	@ApiOperation("获取列表")
    @PostMapping("/list")
    public R list(@RequestBody Map<String, Object> params){
        PageUtils page = tokenConfigService.queryPage(params);
        return R.data(page);
    }

    /**
     * 获取已经配置的列表
     */
	@ApiOperation("获取已经配置的列表")
    @GetMapping("/listToken")
    public R listToken(){
        List<KeyValueVO> page = tokenConfigService.listToken();
        return R.data(page);
    }

//--------------------------------------------------------------------------------------------------------------------
	/**
	 * 获取数据列表
	 * @param params
	 * @return
	 */
	@PostMapping("listData")
	public R listData(@RequestBody Map<String, Object> params){
		PageUtils pageUtils = this.tableManageService.queryPage(params);
		return R.data(pageUtils);
	}

	/**
	 * 删除数据 单个或多个
	 * @param vo
	 * @return
	 */
	@PostMapping("deleteByIds")
	public R deleteData(@RequestBody DeleteVO vo){
		this.tableManageService.deleteByIds(vo.getIds());
		return R.success("刪除成功");
	}

	/**
	 * 获取表名称是否合规
	 * @param vo
	 * @return
	 */
	@PostMapping("getTableNameCompliance")
	public R getTableNameCompliance(@RequestBody TableNameComplianceVO vo){
		HashMap<String, Object> tableNameCompliance = this.tableManageService.getTableNameCompliance(vo.getTableName());
		Boolean o = (Boolean)tableNameCompliance.get("success");
		return R.data(tableNameCompliance);
	}

	/**
	 * 新增或者更新
	 * @param entity
	 * @return
	 * @throws SQLException
	 */
	@PostMapping("saveOrUpdate")
	public R saveOrUpdate(@RequestBody TableManageEntity entity) throws SQLException {
		Boolean aBoolean = this.tableManageService.saveOrUpdateEntity(entity);
		if (aBoolean){
			return R.success("操作成功");
		}
		return R.fail("操作失败");
	}

	/**
	 * 获取表列表
	 * @return
	 */
	@GetMapping("getListToConfig")
	public R getListToConfig(){
		List<TableManageEntity> list = this.tableManageService.list();
		List<HashMap<String,String>> mapList = new ArrayList<>();
		list.stream().forEach(p->{
			HashMap<String, String> hashMap = new HashMap<>();
			hashMap.put("name",p.getName());
			hashMap.put("tableName",p.getTableName());
			mapList.add(hashMap);
		});
		return R.data(mapList);
	}
}
